﻿namespace DataTanker.Settings
{
    /// <summary>
    /// Represents the settings of storage.
    /// </summary>
    public class StorageSettings
    {
        /// <summary>
        /// Gets or sets the access method used to operate with keys and values.
        /// </summary>
        public AccessMethod AccessMethod { get; set; }

        /// <summary>
        /// Gets or sets size of the on-disk page (in bytes).
        /// </summary>
        public PageSize PageSize { get; set; }

        /// <summary>
        /// Gets or sets the page-level caching.
        /// Set this to null to disable caching.
        /// </summary>
        public CacheSettings CacheSettings { get; set; }

        /// <summary>
        /// Gets the value indicating whether all write operations perform immediatly to file storage.
        /// <remarks>
        /// Setting to true disables only OS file system buffer on write operations.
        /// This does not affect the page caching behavior. Disable caching if you don't need it
        /// or call IStorage.Flush() to control the durability of all updates.
        /// </remarks>
        /// </summary>
        public bool ForcedWrites { get; set; }

        /// <summary>
        /// Gets or sets the maximal length of key (in bytes)
        /// </summary>
        public int MaxKeySize { get; set; }

        /// <summary>
        /// Gets or sets the maximal number of released pages.
        /// Vacuum procedure starts immediately when this value is reached.
        /// </summary>
        public int MaxEmptyPages { get; set; }

        /// <summary>
        /// Gets the default storage settings:
        /// AccessMethod = AccessMethod.BPlusTree,
        /// CacheSettings = new CacheSettings
        ///     {
        ///         MaxCachedPages = 3000,
        ///         MaxDirtyPages = 1000
        ///     },
        /// PageSize = PageSize.Default,
        /// ForcedWrites = false,
        /// MaxKeySize = 500,
        /// MaxEmptyPages = 100
        /// </summary>
        /// <returns>Default storage settings</returns>
        public static StorageSettings Default() 
        {
            return new StorageSettings
                       {
                           AccessMethod = AccessMethod.BPlusTree,
                           CacheSettings = new CacheSettings
                                               {
                                                   MaxCachedPages = 3000,
                                                   MaxDirtyPages = 1000
                                               },
                           PageSize = PageSize.Default,
                           ForcedWrites = false,
                           MaxKeySize = 500,
                           MaxEmptyPages = 100
                       };
        }

        /// <summary>
        /// Gets the default storage settings with specified maximal key size.
        /// </summary>
        /// <param name="maxKeySize">Maximal key size</param>
        /// <returns>Default storage settings</returns>
        public static StorageSettings Default(int maxKeySize)
        {
            var settings = Default();
            settings.MaxKeySize = maxKeySize;
            return settings;
        }
    }
}
